package net.piencak.phonetools.eurocoins;

import java.io.IOException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Image;

public class Gallery {

	public static final String[] COUNTRY_NAME = {
		"Spoločná", 		//0
		"Belgicko", 		//1
		"Nemecko", 			//2
		"Írsko", 			//3
		"Grécko", 			//4
		"Španielsko", 		//5
		"Francúzsko", 		//6
		"Taliansko", 		//7
		"Cyprus", 			//8
		"Luxembursko", 		//9
		"Malta", 			//10
		"Holandsko", 		//11
		"Rakúsko", 			//12
		"Portugalsko", 		//13
		"Slovinsko", 		//14
		"Slovensko", 		//15
		"Fínsko", 			//16
		"Monako", 			//17
		"San Maríno", 		//18
		"Vatikán"			//19
	};
	
	public static final String[] COIN_NAME = {
		"2€",				//0
		"1€",				//1
		"50c",				//2
		"20c",				//3
		"10c",				//4
		"5c",				//5
		"2c",				//6
		"1c"				//7
	};

	public static final String[] COIN_DIR = {
		"2E",				//0
		"1E",				//1
		"50c",				//2
		"20c",				//3
		"10c",				//4
		"5c",				//5
		"2c",				//6
		"1c"				//7
	};


	public static final int COUNTRIES_COUNT = COUNTRY_NAME.length - 1;
	public static final int COINS_COUNT = COIN_NAME.length;

	
	private static Image[][] imagePool = new Image[COUNTRIES_COUNT+1][COINS_COUNT];

	public static String getCountryName(int index) {
		return COUNTRY_NAME[index];
	}
	
	private static int getCoinIndex(String coin) {
		if ("2E".equals(coin)) {
			return 0;
		} else if ("1E".equals(coin)) {
			return 1;
		} else if ("50c".equals(coin)) {
			return 2;
		} else if ("20c".equals(coin)) {
			return 3;
		} else if ("10c".equals(coin)) {
			return 4;
		} else if ("5c".equals(coin)) {
			return 5;
		} else if ("2c".equals(coin)) {
			return 6;
		} else if ("1c".equals(coin)) {
			return 7;
		}
		return 0;
	}
	
	public static Image getCoinImage(int country, int coin) {
		if (imagePool[country][coin] == null) {
			try {
				imagePool[country][coin] = Image.createImage("/net/piencak/phonetools/eurocoins/img/" + COIN_DIR[coin] + "/" + country + ".png");
			} catch (IOException e) {
				System.out.println("Error loading image for " + COUNTRY_NAME[country] + "/" + COIN_DIR[coin]);
			} 
		}
		return imagePool[country][coin];
	}
	
	public static void cleanMem(int saveCoin) {
		for (int i = 1; i <= Gallery.COUNTRIES_COUNT; i++) {
			for (int j = 0; j < Gallery.COINS_COUNT; j++) {
				if (j != saveCoin) {
					imagePool[i][j] = null;
				}
			}
		}
		System.gc();
	}

}
